/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.is.city.routemanager;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollection;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.operation.transform.AffineTransform2D;
import org.geotools.swing.JMapPane;
import org.geotools.swing.control.DnDListModel;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.event.MapMouseListener;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

/**
 *
 * @author oleg
 */
public class GuideEditorMapPaneListener implements MapMouseListener {
    private static final ResourceBundle stringRes = ResourceBundle.getBundle("META-INF/Text");
    
    private DnDListModel<Route> listModel;
    private FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
    private JMapPane mapPane;

    public GuideEditorMapPaneListener(DnDListModel<Route> listModel, JMapPane mapPane) {
        this.listModel = listModel;
        this.mapPane = mapPane;
    }

    @Override
    public void onMouseClicked(MapMouseEvent ev) {
        for (int i = 0; i < listModel.getSize(); ++i) {
            try {
                Route route = listModel.getElementAt(i);
                Envelope envelope = new Envelope(new Coordinate(ev.getX(), ev.getY()));
                envelope.expandBy(10);
                AffineTransform screenToWorldTransform = mapPane.getScreenToWorldTransform();
                MathTransform mt = new AffineTransform2D(screenToWorldTransform);
                Envelope transform = JTS.transform(envelope, mt);
                ReferencedEnvelope env = new ReferencedEnvelope(transform, mapPane.getMapContext().getCoordinateReferenceSystem());
                String geometryPropertyName = route.getPointsLayer().getFeatureSource().getSchema().getGeometryDescriptor().getLocalName();
                Filter filter = ff.intersects(ff.property(geometryPropertyName), ff.literal(env));
                FeatureCollection features = route.getPointsLayer().getFeatureSource().getFeatures(filter);
                if (features.size() > 0) {
                    final JPopupMenu menu = new JPopupMenu();

                    // Create and add a menu item
                    JMenuItem item = new JMenuItem(stringRes.getString("Delete_point"));

                    item.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            
                        }
                    });
                    menu.add(item);
                    ((JMapPane)ev.getComponent()).setRepaint(false);
                    menu.show(ev.getComponent(), ev.getX(), ev.getY());
                    ((JMapPane)ev.getComponent()).setRepaint(true);
                }
            } catch (IOException ex) {
                Logger.getLogger(GuideEditorMapPaneListener.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TransformException ex) {
                Logger.getLogger(GuideEditorMapPaneListener.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void onMouseDragged(MapMouseEvent ev) {
    }

    @Override
    public void onMouseEntered(MapMouseEvent ev) {
    }

    @Override
    public void onMouseExited(MapMouseEvent ev) {
    }

    @Override
    public void onMouseMoved(MapMouseEvent ev) {
    }

    @Override
    public void onMousePressed(MapMouseEvent ev) {
    }

    @Override
    public void onMouseReleased(MapMouseEvent ev) {
    }

    @Override
    public void onMouseWheelMoved(MapMouseEvent ev) {
    }
}
